#!/bin/sh 
echo this is ToB RSDK linux  build
function upload_files_toftp()
{
    remote_path=$1
    cd $cur_path/target/
    echo upload all files as follow:
    ls
    echo will create remmote path $remote_path and upload files to it
    curl -u aolingli:ALL1234! $1 --ftp-create-dirs #创建远程目录
    for upload_file in `ls`
        do
            echo upload_file=$upload_file
            curl -u aolingli:ALL1234! -T "$upload_file"  $1
        done
    rm $cur_path/target/* -rf

}
source /etc/profile
echo jenkins build_user_id=$BUILD_USER_ID and branch=$1
trigger_user=$BUILD_USER_ID
[ "x$BUILD_USER_ID" == "x" ] && trigger_user=$BUILD_USER
[ "x$BUILD_USER" == "x" ] && trigger_user=Timer
echo user $trigger_user trigger this build
build_tag=`cat build_info |grep released_tag |awk -F "=" '{print $2}'`
if [ "x$build_tag" != "x" ]; then 
    [ "x`git tag |grep "$build_tag"`" == "x" ] && echo cannot find tag $build_tag  && exit 1
    echo checking out tag: $build_tag
    git checkout $build_tag
    bt_name=$build_tag
else
    echo checking out branch $1
    git checkout $1
    git pull
    bt_name=$1
fi
bt_name=`echo $bt_name |sed 's/\.//g'` #删除分支名或者tag中的点符号
dir_name="`date "+%Y%m%d%H%M"`"_"$bt_name"_"`git rev-parse --short HEAD`"_"$trigger_user"
ftp_name=ftp://192.168.8.251/output/ToB-RSDK/$dir_name
cur_path=`pwd`
build_shell_path=$cur_path/build/linux/
[ -d target ] && echo delete old target && rm target -rf 
mkdir target
mkdir target_all
cd $build_shell_path
dos2unix *.sh
chmod 777 *.sh
sh clean.sh
sh build_hs_a.sh |tee build_hs_a_result #编译静态库
[  ! -d $cur_path/lib/linux/arm_hs320/ ] && echo  $cur_path/lib/linux/arm_hs320/ not exist && exit 1
[ ! -f $cur_path/bin/linux/arm_hs320/lbapp_static ] && echo $cur_path/bin/linux/arm_hs320/lbapp_static not exist and build_hs_a.sh build failed && exit 1
cp $cur_path/lib/linux/arm_hs320/*  $cur_path/target/ #回收结果文件
cd $cur_path/framecore/poco/linux/arm_hs320/lib/
cp libPocoCrypto.a libPocoEncodings.a libPocoFoundation.a libPocoJSON.a libPocoNet.a libPocoNetSSL.a libPocoUtil.a libPocoXML.a $cur_path/target/
cp $cur_path/third/airplay/linux/lib/arm_hs320/* $cur_path/target/
cp $cur_path/third/hssdk/lib/arm_hs320/libexif/libexif.a $cur_path/target/
cp $cur_path/third/aac/lib/linux/arm_hs320/libfdk-aac.a $cur_path/target/
echo create remote $ftp_name
curl -u aolingli:ALL1234! $ftp_name/ --ftp-create-dirs #创建外层远程目录
mkdir -p $cur_path/target_all/lib/linux/arm_hs320
cp -rn $cur_path/target/* $cur_path/target_all/lib/linux/arm_hs320/
upload_files_toftp $ftp_name/lib/linux/arm_hs320/
echo ================ build_hs_a.sh done ============

cd $build_shell_path
sh clean.sh
sh build_hs.sh |tee build_hs_result #编译动态库
[ ! -d $cur_path/bin/linux/arm_hs320/ ] && echo $cur_path/bin/linux/arm_hs320/ not exist && exit 1
cp $cur_path/bin/linux/arm_hs320/* $cur_path/target/
cp $cur_path/third/mdns/linux/bin/arm_hs320/mdnsd $cur_path/target/
cp $cur_path/third/mdns/linux/bin/arm_hs320/libdns_sd.so $cur_path/target/
rm $cur_path/target/demo $cur_path/target/uidemo
mkdir -p $cur_path/target_all/bin/linux/arm_hs320
cp -rn $cur_path/target/* $cur_path/target_all/bin/linux/arm_hs320/
upload_files_toftp  $ftp_name/bin/linux/arm_hs320/
echo ================ build_hs.sh done ============

cd $cur_path
cp include/ircast.h include/itype.h $cur_path/target/
mkdir -p $cur_path/target_all/include
cp -rn $cur_path/target/* $cur_path/target_all/include/
upload_files_toftp $ftp_name/include/
echo ================ copy include done ============

cd $cur_path/build/linux/test/demo/
cp makefile mapfile $cur_path/target/
mkdir -p $cur_path/target_all/build/linux/test/demo
cp -rn $cur_path/target/* $cur_path/target_all/build/linux/test/demo/
upload_files_toftp $ftp_name/build/linux/test/demo/
echo ================ copy build/linux/test/demo done ============

cd $cur_path/build/linux/
cp config.mak  $cur_path/target/
cp -rn $cur_path/target/* $cur_path/target_all/build/linux/
upload_files_toftp $ftp_name/build/linux/
echo ================ copy build/linux done ============

cd $cur_path/test/demo/
cp demo.cpp demo.h hssdk.cpp hssdk.h $cur_path/target
mkdir -p $cur_path/target_all/test/demo
cp -rn $cur_path/target/* $cur_path/target_all/test/demo/
upload_files_toftp $ftp_name/test/demo/
echo ================ copy test/demo done ============

cd $cur_path
echo ftp_path=$ftp_name > result_ftp_path.txt
cd target_all
zip -r "$dir_name".zip  *
cp "$dir_name".zip $cur_path/target/
#上传完整路径压缩包到ftp上
upload_files_toftp $ftp_name/
echo ftp_path=$ftp_name/
